﻿using System;
using System.Web;
using System.Collections.Generic;
using System.Collections;
using System.Linq;

using Tools.Json;

using SY.WebSite.BLL;
using SY.WebSite.Entity;

namespace SY.WebSite.Web.Handler
{
    public class LotteryManageHandler : AdminWebHandler, IHttpHandler
    {
        public new void ProcessRequest(HttpContext context)
        {
            base.ProcessRequest(context);

            switch (Action)
            {
                case "getList":
                    GetList();
                    break;
                case "add":
                    Add();
                    break;
                case "edit":
                    Edit();
                    break;
                case "setLuckyOrForbidden":
                    SetLuckyOrForbidden();
                    break;
                case "cancel":
                    Cancel();
                    break;
            }

            context.Response.End();
        }

        public void SetLuckyOrForbidden()
        {
            int id = Tools.Web.Request.GetFormInt("id", 0);
            string userList = Tools.Web.Request.GetFormString("list");
            int type = Tools.Web.Request.GetFormInt("type", 0);

            if (id <= 0 || type <= 0 || string.IsNullOrEmpty(userList))
                BreakResponse();

            List<Lottery> list = BizLottery.GetAll().ToList();

            Lottery lottery = list.FirstOrDefault(l => l.Id == id && l.Status == 0 && l.OpenTime <= DateTime.Now);
            if (lottery == null)
                BreakResponse();

            string temp = string.Empty;
            if (type == 1)
            {
                temp = lottery.Lucky;
                lottery.Lucky = "," + userList + ",";
            }
            else if (type == 2)
            {
                temp = lottery.Forbidden;
                lottery.Forbidden = "," + userList + ",";
            }

            int result = BizLottery.Update(lottery);
            if (result > 0)
            {
                Response.Write(new JsonTransfer().Add("success", ""));
                Response.End();
            }
            if (type == 1)
            {
                lottery.Lucky = temp;
            }
            else if (type == 2)
            {
                lottery.Forbidden = temp;
            }
            BreakResponse();
        }
        
        public void Cancel()
        {
            int id = Tools.Web.Request.GetFormInt("id", 0);

            if (id <= 0)
                BreakResponse();

            List<Lottery> list = BizLottery.GetAll().ToList();

            Lottery lottery = list.FirstOrDefault(l => l.Id == id && l.Status == 0);
            if (lottery == null)
                BreakResponse();

            if (lottery.OpenTime <= DateTime.Now || !string.IsNullOrEmpty(lottery.Participants))
                BreakResponse("待开奖状态不能删除");

            lottery.Status = -1;
            int result = BizLottery.Update(lottery);
            if (result > 0)
            {
                Response.Write(new JsonTransfer().Add("success", ""));
                Response.End();
            }

            BreakResponse();
        }

        public void Edit()
        {
            int id = Tools.Web.Request.GetFormInt("id", 0);
            string title = Tools.Web.Request.GetFormString("title");
            string logo = Tools.Web.Request.GetFormString("logo");
            string end = Tools.Web.Request.GetFormString("end");

            if (id <= 0 || string.IsNullOrEmpty(title) || string.IsNullOrEmpty(logo) || string.IsNullOrEmpty(end))
                BreakResponse();

            DateTime endTime = DateTime.Now;
            if (!DateTime.TryParse(end, out endTime))
                BreakResponse();

            List<Lottery> list = BizLottery.GetAll().ToList();

            Lottery lottery = list.FirstOrDefault(l => l.Id == id && l.Status == 0);
            if (lottery == null)
                BreakResponse();

            if (lottery.OpenTime <= DateTime.Now)
                BreakResponse("待开奖状态不能再编辑基本信息");

            lottery.Title = Tools.StringUtil.HtmlEncode(title);
            lottery.OpenTime = endTime;
            lottery.Image = Tools.StringUtil.HtmlEncode(logo);

            int result = BizLottery.Update(lottery);
            if (result > 0)
            {
                Response.Write(new JsonTransfer().Add("success", ""));
                Response.End();
            }

            BreakResponse();
        }

        public void Add() {
            string title = Tools.Web.Request.GetFormString("title");
            string logo = Tools.Web.Request.GetFormString("logo");
            string end = Tools.Web.Request.GetFormString("end");
            int limitAmount = Tools.Web.Request.GetFormInt("limitAmount", 0);
            int costAmount = Tools.Web.Request.GetFormInt("costAmount", 0);

            if (string.IsNullOrEmpty(title) || string.IsNullOrEmpty(logo) || string.IsNullOrEmpty(end) || limitAmount < 0 || costAmount < 0)
                BreakResponse();

            DateTime endTime = DateTime.Now;
            if (!DateTime.TryParse(end, out endTime))
                BreakResponse();

            Lottery lottery = new Lottery
            {
                Title = title,
                Image = logo,
                OpenTime = endTime,
                LimitAmount = limitAmount,
                CostAmount = costAmount,
                UserId = CurrentUser.UserId,
                UserName = CurrentUser.UserName
            };

            int result = BizLottery.Add(lottery);
            if (result > 0)
            {
                Response.Write(new JsonTransfer().Add("success", ""));
                Response.End();
            }

            BreakResponse();
        }

        public void GetList()
        {
            int pageIndex = RequestIntParam("pageIndex");
            int pageSize = RequestIntParam("pageSize", 20);

            string title = RequestStringParam("title");
            int spstatus = RequestIntParam("spstatus", 100);
            string start = RequestStringParam("start");
            string end = RequestStringParam("end");

            if (spstatus < 0)
                BreakResponse();

            JsonTransfer jt = new JsonTransfer();
            IEnumerable<Lottery> list = BizLottery.GetAll();
            switch (spstatus) { 
                case 0:
                    list = list.Where(l => l.OpenTime > DateTime.Now);
                    break;
                case 1:
                    list = list.Where(l => l.Status == 0 && l.OpenTime <= DateTime.Now);
                    break;
                case 2:
                    list = list.Where(l => l.Status == 1);
                    break;
            }

            if (!string.IsNullOrEmpty(title))
                list = list.Where(l => l.Title.Contains(title));

            DateTime temp;
            if (!string.IsNullOrEmpty(start) && DateTime.TryParse(start, out temp))
                list = list.Where(l => l.CreateTime >= temp);

            DateTime endTemp;
            if (!string.IsNullOrEmpty(end) && DateTime.TryParse(end, out endTemp))
                list = list.Where(l => l.OpenTime <= endTemp);

            jt.Add("list", list.Skip(pageIndex * pageSize).Take(pageSize));
            jt.Add("count", list.Count());
            Response.Write(jt);
        }
    }
}